$(function(){
	//随机产生验证码 
	function refresh(){
		var verifyCode = new String();
	    for(var i=0; i<4;i++){
	    	verifyCode += parseInt(Math.random()*10);
	    }
		$('.user-info li:nth-child(3) span').html(verifyCode);
		$.cookie("verifyCode",verifyCode)
	}
	refresh()  
	$('.user-info li:nth-child(3) span').click(function(){
		refresh()
	})
	$('.refresh').click(function(){
		refresh()
	})
	//  验证码验证
	$('.user-info li:nth-child(3) input').blur(function(){
		console.log($.cookie("verifyCode"));
		if($(this).val()!=$.cookie("verifyCode")){
			$(this).val(null);
			alert("验证码不正确");
		}
	})
	
	//  是否有保存登录记录
	
	if($.cookie('loginRemember')=='true'){
		var remember = JSON.parse($.cookie("remember"));
//		console.log(remember)
		$('#username').val(remember.username);
		$('#password').val(remember.password);
	}
	
	var loginRemember = 'false';
//	$.cookie('loginRemember',"false",{path: "/", expires: 10})
	// 登录验证
	$('button').click(function(){
		$.cookie('obj')
		
		var obj = JSON.parse($.cookie("obj"));
	    var cheak="true";
	    for(var i = 0; i<obj.length; i++){
	    	var index = i;
	    	if($('#username').val()==obj[i].username){
	    		if($('#password').val()==obj[i].password){
	    			alert('登录成功');
	    			//  记住我
	    			if($(".remember").is(':checked')){
	    				loginRemember = 'true';
	    				$.cookie('loginRemember',loginRemember,{path: "/", expires: 10})
	    				var remember = new Object();
	    				remember.username = $('#username').val();
	    				remember.password = $('#password').val();
	    				cheak="true";
	    				$.cookie("remember",JSON.stringify(remember),{
			                path: "/", expires: 10
			            })
	    				break;
//	    				console.log($.cookie("remember"))
	    			}
	    		}else{
	    			alert('密码错误');
	    			break;
	    		}
	    	}else{
	    		cheak = 'false'
	    		continue;
	    		
	    	}
	    }
	    if(cheak=="false"){
	    	alert('用户名不存在');
	    }
		
	})
	
})
